package com.screens;

import sheep.game.Sprite;
import sheep.game.State;
import sheep.gui.TextButton;
import android.graphics.Canvas;
import android.view.MotionEvent;

import com.bricks2break.Level;
import com.bricks2break.Statics;

/**
 * New screen for Start menu. This is where you choose a level, start a game, view brick properties and exit the game.
 * This is also where you get when you push the back button.
 * This is a class with singleton pattern.
 */
public class StartMenu extends State{ 

	/**
	 * Creates the objects.
	 */
	private static StartMenu instance = null;
	private TextButton startGameButton;
	private TextButton brickPropertiesButton;
	private TextButton exitGameButton;
	private Sprite logo;
	private TextButton chooseBoard;
	private Sprite lvlPlus;
	private Sprite lvlMinus;
	private TextButton lvlNumberTxt;
	private int lvlNumber;
	
	/**
	 * Instantiates the objects.
	 */
	public StartMenu(){		
		lvlNumber = 0;
		startGameButton = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.45f,Statics.string_start_game);
		brickPropertiesButton = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.6f,Statics.string_brick_properties);
		exitGameButton = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.8f,Statics.string_exit_game);
		
		chooseBoard = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.4f,Statics.string_choose_board);
		lvlPlus = new Sprite(Statics.image_lvlplus);
		lvlNumberTxt = new TextButton(Statics.SCREEN_WIDTH*0.70f, Statics.SCREEN_HEIGHT*0.4f, ""+(lvlNumber+1));
		lvlMinus = new Sprite(Statics.image_lvlminus);
		
		logo = new Sprite(Statics.image_logo);
		logo.setPosition(Statics.SCREEN_WIDTH*0.5f, Statics.SCREEN_HEIGHT*0.2f);
		lvlPlus.setPosition(Statics.SCREEN_WIDTH*0.78f, Statics.SCREEN_HEIGHT*0.4f);
		lvlMinus.setPosition(Statics.SCREEN_WIDTH*0.65f, Statics.SCREEN_HEIGHT*0.4f);

		addTouchListener(startGameButton);
		addTouchListener(brickPropertiesButton);
		addTouchListener(exitGameButton);
		addTouchListener(this);
	}
	
	/**
	 * Because of the singleton pattern this method returns the instance of this class,
	 * and makes sure that only one instance is made. 
	 * @return instance
	 */
	public static StartMenu getInstance(){
		if (instance == null){
			instance = new StartMenu();
		}
		return instance;
	}
	
	/**
	 * Updates the objects.
	 * @param dt
	 */
	public void update(float dt){
		super.update(dt);
		lvlNumberTxt.setLabel(""+(lvlNumber+1));
		logo.update(dt);
		lvlMinus.update(dt);
		lvlPlus.update(dt);
	}
	
	/**
	 * Draws the objects.
	 * @param canvas
	 */
	public void draw(Canvas canvas){
		super.draw(canvas);
		canvas.drawRect(0, 0, Statics.SCREEN_WIDTH, Statics.SCREEN_HEIGHT, Statics.black);
		logo.draw(canvas);
		startGameButton.draw(canvas);
		brickPropertiesButton.draw(canvas);
		exitGameButton.draw(canvas);
		chooseBoard.draw(canvas);
		lvlMinus.draw(canvas);
		lvlPlus.draw(canvas);
		lvlNumberTxt.draw(canvas);	
	}
	
	/**
	 * 
	 * @param a
	 * @param event
	 * @return
	 */
	public boolean isTouched(Sprite a, MotionEvent event){
		if(event.getX() <= a.getX()+Statics.image_lvlplus.getWidth()/2 && event.getX() >= a.getX()-Statics.image_lvlplus.getWidth()/2 && event.getY() <= a.getY()+Statics.image_lvlplus.getHeight()/2 && event.getY() >= a.getY()-Statics.image_lvlplus.getHeight()/2){
			return true;
		}
		return false;
	}
	
	
	/**
	 * Defines what happens when the screen is touched.
	 * @param event
	 */
	public synchronized boolean onTouchDown(MotionEvent event) {
		if(isTouched(lvlPlus, event)){
			if(lvlNumber < Statics.bricks.length-1){
				lvlNumber++;
				return true;
			}
		}
		else if(isTouched(lvlMinus, event)){
			if(lvlNumber > 0){
				lvlNumber--;
				return true;
			}
		}
		else if(startGameButton.getBoundingBox().contains(event.getX(), event.getY())){
			getGame().pushState(new Level(lvlNumber));
			return true;
		}
		else if(brickPropertiesButton.getBoundingBox().contains(event.getX(), event.getY())){
			getGame().pushState(new BrickProperties());
			return true;
		}
		else if(exitGameButton.getBoundingBox().contains(event.getX(), event.getY())){
			getGame().popState();
			System.exit(0);
			return true;
		}
		return false;
	}
	
	public synchronized boolean onTouchMove(MotionEvent event){
		return true;
	}
	
	public synchronized boolean onTouchUp(MotionEvent event){
		return true;
	}
}
